/********************黄涛**************************/
const {
    lessorsModel
} = require("./models/lessorsModel");

const {
    tenantsModel
} = require("./models/tenantsModel");

const {
    parkingsModel
} = require("./models/parkingsModel");

//获取出租方信息
module.exports.getLessors = async (req) => {
    const {
        type,
        value,
        currentPage,
        pageSize
    } = req
    const totalitem = await (await lessorsModel.find({
        [type]: {
            $regex: value
        }
    })).length;
    const pagecount = Math.ceil(((await lessorsModel.find({
        [type]: {
            $regex: value
        }
    })).length) / (pageSize - 0));
    const pagerows = await lessorsModel.find({
        [type]: {
            $regex: value
        }
    }).limit(pageSize - 0).skip((pageSize) * (currentPage - 1));
    return {
        status: 1,
        message: "获取成功",
        data: {
            totalitem,
            pagecount,
            pagerows,
        }
    }
}

module.exports.modifyLessors = async (data) => {//单独的修改出租方的state
    const {
        _id,
        state
    } = data;
    const result = await lessorsModel.updateOne({
        _id
    }, {
        state
    });
    if (result.n) {
        return {
            status: 1,
            message: "成功"
        }
    }
    else {
        return {
            status: 0,
            message: "失败"
        }
    }
}

module.exports.EditLessors = async (data) => {//单独的修改出租方的state
    const { _id } = data;
    const result = await lessorsModel.updateOne({ _id }, data);
    if (result.n) {
        return {
            status: 1,
            message: "成功"
        }
    } else {
        return {
            status: 0,
            message: "失败"
        }
    }
}

module.exports.deleteLessors = async (req) => {
    try {
        const result = await lessorsModel.deleteOne(req);
        if (result.n) {
            return {
                status: 1,
                message: "刪除成功"
            }
        }
    } catch (error) {
        return {
            status: 0,
            message: "刪除失敗"
        }

    }


}
//车位
module.exports.getParkings = async (req) => {
    const {
        type,
        value,
        currentPage,
        pageSize
    } = req
    const totalitem = await (await parkingsModel.find({
        [type]: {
            $regex: value
        }
    })).length;
    const pagecount = Math.ceil(((await parkingsModel.find({
        [type]: {
            $regex: value
        }
    })).length) / (pageSize - 0));
    const pagerows = await parkingsModel.find({
        [type]: {
            $regex: value
        }
    }).populate({path:"lessorsId"}).limit(pageSize - 0).skip((pageSize) * (currentPage - 1));
    return {
        status: 1,
        message: "获取成功",
        data: {
            totalitem,
            pagecount,
            pagerows,
        }
    }
}
module.exports.modifyParkings = async (data) => {//单独的修改出租方的state
    const { _id, state } = data;
    const result = await parkingsModel.updateOne({ _id }, { state });
    if (result.n) {
        return {
            status: 1,
            message: "成功"
        }
    } else {
        return {
            status: 0,
            message: "失败"
        }
    }
}

module.exports.EditParkings = async (data) => {//单独的修改出租方的state
    const { _id } = data;
    const result = await parkingsModel.updateOne({ _id }, data);
    if (result.n) {
        return {
            status: 1,
            message: "成功"
        }
    } else {
        return {
            status: 0,
            message: "失败"
        }
    }
}



module.exports.deleteParkings = async (req) => {
    try {
        const result = await parkingsModel.deleteOne(req);
        if (result.n) {
            return {
                status: 1,
                message: "刪除成功"
            }
        }
    } catch (error) {
        return {
            status: 0,
            message: "刪除失敗"
        }

    }


}

//租客
module.exports.getTenants = async (req) => {
    const {
        type,
        value,
        currentPage,
        pageSize
    } = req
    const totalitem = await (await tenantsModel.find({
        [type]: {
            $regex: value
        }
    })).length;
    const pagecount = Math.ceil(((await tenantsModel.find({
        [type]: {
            $regex: value
        }
    })).length) / (pageSize - 0));
    const pagerows = await tenantsModel.find({
        [type]: {
            $regex: value
        }
    }).limit(pageSize - 0).skip((pageSize) * (currentPage - 1));
    return {
        status: 1,
        message: "获取成功",
        data: {
            totalitem,
            pagecount,
            pagerows,
        }
    }
}
module.exports.modifyTenants = async (data) => {//单独的修改租客的认证的state
    const {_id,state} = data;
    const result = await   tenantsModel.updateOne({_id}, {state});
    if(result.n){
        return {
            status:1,
            message:"成功"
        }
    } else{
        return {
            status:0,
            message:"失败"
        }
    }
}

module.exports.EditTenants = async (data) => {//单独的修改出租方的state
    const { _id } = data;
    const result = await tenantsModel.updateOne({ _id }, data);
    if (result.n) {
        return {
            status: 1,
            message: "成功"
        }
    } else {
        return {
            status: 0,
            message: "失败"
        }
    }
}


module.exports.deleteTenants = async (req) => {
    try {
        const result = await tenantsModel.deleteOne(req);
        console.log(result.n == 1);
        if (result.n == 1) {
            return {
                status: 1,
                message: "刪除成功"
            }
        }
    } catch (error) {
        return {
            status: 0,
            message: "失败"
        }

    }
}

/********************黄涛**************************/
const { adminsModel } = require('./models/adminsModel');

//获取管理员数据
module.exports.getAdmins = async (_id) => {
    return await adminsModel.find(_id);
}

//删除管理员
module.exports.deleteAdmins = async (_id) => {
    const data = await adminsModel.findByIdAndDelete(_id);
    return data;

}

//禁用管理员
module.exports.forbidAdmins = async ({ _id, state }) => {
    // 改变管理员状态
    if (state == 1) {
        state = 2;
    } else {
        state = 1;
    }
    const data = await adminsModel.findByIdAndUpdate(_id, { state });
    return data;
}

//新增管理员
module.exports.addAdmins = async ({ password, phone, account }) => {
    const data = await adminsModel.create({ password, phone, account });
    return data;
}

//修改管理员数据
module.exports.modifyAdmins = async ({ _id, password, phone, account }) => {
    const data = await adminsModel.findByIdAndUpdate(_id, { password, phone, account });
    return data;
}

//验证管理员账号
module.exports.isAdmins = async (account) => {
    const data = await adminsModel.find(account);
    return data;
}

//查找管理员
module.exports.seekAdmins = async ({ status, value, type, pageSize, currentPage }) => {
    let data = [];
    let total = '';
    if (type == "state") {
        data = await adminsModel.find({ [type]: status }).limit(pageSize).skip((currentPage - 1) * pageSize);
        total =(await adminsModel.find({ [type]: status })).length;
    } else if (type == '') {
        data = await adminsModel.find().limit(pageSize).skip((currentPage - 1) * pageSize);
        total =(await adminsModel.find()).length;
    }
    else {
        data = await adminsModel.find({ [type]: { $regex: value, $options: '$i' } }).limit(pageSize).skip((currentPage - 1) * pageSize);
        total =(await adminsModel.find({ [type]: { $regex: value, $options: '$i' } })).length;
    }
    return { data, total};
}
